var vm = new Vue({
    el: '#app',
    data: {
        host,
        username: '',
        mobile: '',
        email: '',
        email_active: false,
        set_email: false,
        send_email_btn_disabled: false,
        send_email_tip: '重新发送验证邮件',
        email_error: false,
        histories: [],
        orders: [],
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        order_id: '',
        amount: 0,
        pay_method: '',
    },
    mounted: function () {
        // 获取cookie中的用户名
        this.order_id = this.get_query_string('order_id');
        this.amount = this.get_query_string('amount');
        this.pay_method = this.get_query_string('pay');

        this.username = getCookie('username');

        // 获取个人信息:
        this.get_person_info()

        this.get_history()

        this.get_user_order()
    },
    methods: {
          // 退出登录按钮
        logoutfunc: function () {
            var url = this.host + '/logout/';
            axios.delete(url, {
                responseType: 'json',
                withCredentials:true,
            })
                .then(response => {
                    location.href = 'login.html';
                })
                .catch(error => {
                    console.log(error.response);
                })
        },
        //获取路径参数
        get_query_string: function(name){
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },
        get_history:function(){
             // 添加下列代码, 发送请求, 获取用户的浏览记录信息:
            axios.get(this.host + '/browse_histories/', {
                    responseType: 'json',
                    withCredentials:true,
                })
                .then(response => {
                    this.histories = response.data.skus;
                    for(var i=0; i<this.histories.length; i++){
                      this.histories[i].url='/goods/'+this.histories[i].id + '.html';
                    }
                })
                .catch(error => {
                    console.log(error)
                });
        },
        // 获取用户所有的资料
        get_person_info: function () {
            var url = this.host + '/info/';
            axios.get(url, {
                responseType: 'json',
                withCredentials: true
            })
                .then(response => {
                    if (response.data.code == 400) {
                        location.href = 'login.html'
                        return
                    }
                    this.username = response.data.info_data.username;
                    this.mobile = response.data.info_data.mobile;
                    this.email = response.data.info_data.email;
                    this.email_active = response.data.info_data.email_active;
                })
                .catch(error => {
                    this.set_email = false
                    location.href = 'login.html'
                })
        },
        // 获取所有订单信息
        get_user_order: function(){
            var url = this.host + '/orders/info/';
            axios.get(url, {
                responseType: "json",
                withCredentials: true
            })
                .then(response => {
                    if (response.data.code == 400) {
                        alert("获取订单信息失败")
                        return
                    }
                    // 获取订单数据
                    this.orders = response.data.orders;
                })
                .catch(error => {
                    console.log(error.response.data);
                    alert('获取订单信息失败');
                })
        },
        get_order_id: function(){
            var url = this.host + '/orders/id/';
            axios.get(url, {
                responseType: "json",
                withCredentials: true
            })
                .then(response => {
                    if (response.data.code == 400) {
                        alert("获取订单信息失败")
                        return
                    }
                    // 获取订单数据
                    this.order_id = response.data.order_id;
                })
                .catch(error => {
                    console.log(error.response.data);
                    alert('获取订单信息失败');
                })
        },
        next_operate: function(order_id){
            if (this.pay_method == 1) {
                location.href = '/index.html';
            } else {
                // 发起支付
                // this.get_order_id()
                var url = this.host+'/payment/'+order_id+'/'
                axios.get(url, {
                        withCredentials:true,
                        responseType: 'json'
                    })
                    .then(response => {
                        // 跳转到支付宝支付
                         if (response.data.code == 0) {
                            location.href = response.data.alipay_url;
                        } else {
                            alert(response.data.errmsg)
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    })
            }
        }
        // 保存email
        // save_email: function () {
        //     var re = /^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
        //     if (re.test(this.email)) {
        //         this.email_error = false;
        //     } else {
        //         this.email_error = true;
        //         return;
        //     }

        //     // 进行前端页面请求:
        //     var url = this.host + '/emails/'
        //     axios.put(url,
        //         {
        //             email: this.email
        //         },
        //         {
        //             responseType: 'json',
        //             withCredentials:true,
        //         })
        //         // 成功请求的回调
        //         .then(response => {
        //             this.set_email = false;
        //             this.send_email_btn_disabled = true;
        //             this.send_email_tip = '已发送验证邮件'
        //         })
        //         // 失败请求的回调:
        //         .catch(error => {
        //             alert('请求失败, 失败原因:', error);
        //         });
        // }
    }
});